package tool

import dao.ModeDao
import utils.Utils

import java.io.File
import java.nio.file.Files

/**
 * Created by yz on 7/5/2021
 */
object Tool {

  val projectName = "exorbase_v2"
  val dbName = s"${projectName}_database"
  val windowsPath = s"E:\\${dbName}"
  val playPath = new File("../").getAbsolutePath
  val linuxPath = playPath + s"/${dbName}"
  val path = {
    if (Utils.isWindows) windowsPath else linuxPath
  }
  val dataDir = new File(path, "data")
  val downloadDir = new File(dataDir, "Downloads")
  val windowsTestDir = new File("C:\\temp")
  val linuxTestDir = new File(playPath, "workspace")
  val testDir = if (windowsTestDir.exists()) windowsTestDir else linuxTestDir
  val rPath = {
    val rPath = s"C:\\workspaceForIDEA\\${projectName}\\server\\rScripts"
    val linuxRPath = linuxPath + "/rScripts"
    if (new File(rPath).exists()) rPath else linuxRPath
  }

  def isTestMode(implicit modeDao: ModeDao) = {
    val mode = Utils.execFuture(modeDao.select)
    if (mode.test == "t") true else false
  }

  def getTargetFile(kind: String, id: String) = {
    new File(Tool.dataDir, s"${kind}/expression/${id}.csv")
  }

  def createTempDirectory(prefix: String)(implicit modeDao: ModeDao) = {
    if (isTestMode) Tool.testDir else Files.createTempDirectory(prefix).toFile
  }

  def deleteDirectory(direcotry: File)(implicit modeDao: ModeDao) = {
    if (!isTestMode) Utils.deleteDirectory(direcotry)
  }

  def getTrueColumnName(x: String) = {
    val r = "([A-Z])".r
    r.replaceAllIn(x, (matcher) =>
      s"_${matcher.group(1).toLowerCase}"
    )
  }


}
